package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.business.api.RemoteStrategyEsService;
import cn.wolfcode.wolf2w.business.api.domain.*;
import cn.wolfcode.wolf2w.business.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.business.service.IStrategyContentService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.security.annotation.InnerAuth;
import cn.wolfcode.wolf2w.business.query.StrategyQuery;
import cn.wolfcode.wolf2w.business.service.IStrategyService;
import cn.wolfcode.wolf2w.common.security.annotation.RequiresLogin;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 攻略 Controller
 * 
 * @author zty
 * @date 2025-09-16
 */
@RestController
@RequestMapping("strategies")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Resource
    private IStrategyContentService strategyContentService;
    @Autowired
    private RemoteStrategyEsService remoteStrategyEsService;
    /**
     * 攻略详情
     */
    @GetMapping("/detail/{id}")
    public R<Strategy> detail(@PathVariable Long id) {
        Strategy strategy = strategyService.getById(id);
        strategy.setContent(strategyContentService.getById(id));
        return R.ok(strategy);
    }
   /* 攻略 列表+条件查询*/
    @GetMapping("/query")
    public R<IPage<Strategy>> query(StrategyQuery qo,
                                    @RequestParam(name = "orderBy", required = false) String orderBy,
                                    @RequestParam(name = "currentPage", required = false, defaultValue = "1") int currentPage,
                                    @RequestParam(name = "refid", required = false) Integer refId,
                                    @RequestParam(name = "type", required = false) Integer type) {
        IPage<Strategy> page = strategyService.queryPage(qo,orderBy,currentPage, refId, type);
        return R.ok(page);
    }
        @GetMapping("/catalogList")
        public R<List<StrategyCatalog>> catalogList(Long destId) {
         List<StrategyCatalog> catalogList =   strategyCatalogService.queryCatalogList(destId);
            return R.ok(catalogList);
        }
        /*获取热门攻略榜单前三*/
    @GetMapping("/viewnnumTop3")
    public R<List<Strategy>> viewnnumTop3(Long destId) {
        List<Strategy> viewnnumTop3 = strategyService.strategyTop3(destId);
        return R.ok(viewnnumTop3);
    }
    @GetMapping("/viewnnumTop5")
    public R<List<Strategy>> viewnnumTop3() {
        List<Strategy> viewnnumTop5 = strategyService.strategyTop5();
        return R.ok(viewnnumTop5);
    }

    /*浏览量自增*/
    @PostMapping("/viewnumIncr")
    public R<Map<String,Object>> viewnumIncr(Long sid){
        Map<String,Object> map = strategyService.viewnumIncr(sid);
        return R.ok(map);
    }
    /*评论数自增*/
    @PostMapping("/replynumIncr")
    public R<Map<String,Object>> replynumIncr(Long sid) {
        Map<String, Object> map = strategyService.replynumIncr(sid);
        return R.ok(map);
    }
    /*收藏*/
    @PostMapping("/favor")
    @RequiresLogin
    public R<Map<String,Object>> favor( Long sid) {
        Map<String, Object> map = strategyService.favor( sid);
        return R.ok(map);
    }
    /*校验是否是用户已经收藏的*/
    @GetMapping("/isUserFavor")

    public R<Boolean> isUserFavor(Long uid,Long sid) {
      Boolean b=  strategyService.isUserFavor(uid,sid);
        return R.ok(b);
    }
    /*点赞功能--一日最多三次*/
    @PostMapping("/thumbsup")
    @RequiresLogin
    public R<Map<String,Object>> thumbsup(Long sid) {
        Map<String, Object> map = strategyService.thumbsup( sid);
        return R.ok(map);
    }

    @GetMapping("/dataInit")
    public R<?> dataInit() {
        for (Strategy strategy : strategyService.list()) {
            StrategyES es = new StrategyES();
            //同名属性拷贝
            BeanUtils.copyProperties(strategy, es);
            remoteStrategyEsService.initStrategyEs(es, SecurityConstants.INNER);


        }
        return R.ok("攻略数据初始化完成");

    }


    /*****************************************对外暴露Fegin接口**********************************************/
    /**
     * Feign 接口
     */
    @GetMapping("/feign/list")
    public R<List<Strategy>> feignList() {
        return R.ok(strategyService.list());
    }
    @InnerAuth
    @GetMapping("/feign/{id}")
    public R<Strategy> feignGet(@PathVariable Long id) {
        return R.ok(strategyService.getById(id));
    }
    /*定时更新攻略排行数据*/
    @PostMapping("/feign/statisRank")
    public R<?> statisRank(){
        System.out.println("更新开始");
        strategyService.statisRank();
        System.out.println("更新结束--成功");
        return R.ok();
    }
    /*定时更新攻略导航正文数据*/
    @PostMapping("/feign/statisCondition")
    public R<?> statisCondition(){
        System.out.println("更新开始");
       strategyService.statisCondition();
        System.out.println("更新结束--成功");
        return R.ok();
    }
    /*定时统计数据持久化*/
    @PostMapping("/feign/strategyStatisHashPersistence")
    public R<?> strategyStatisHashPersistence(){
        //System.out.println("更新开始");
        strategyService.strategyStatisHashPersistence();
        //System.out.println("更新结束--成功");
        return R.ok();

    }

}
